﻿
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Caching.Memory;
using OA.Model;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using X.PagedList;

namespace OA.Web.Areas.Member.Controllers
{
     
    public class DelegationController : BaseMemberController
    {
        private IMemoryCache _cache;
        private ICookie _cookie;
        public DelegationController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
            _cookie = cookie;
        }

        [MyActionFilterAttribute("001005", "工作委托", "个人中心", true, "iconOA_gongzuoweituo")]
        public IActionResult Index()
        {
            return View();
        }

        [MyActionFilterAttribute("001005", "工作委托", "个人中心", true, "iconOA_gongzuoweituo")]
        public IActionResult GetData(int page = 1, int limit=20)
        {
            var list = this.dbContextFactory.DbContext.MyDelegation.Where(m => m.FromEmpID == Info.EmpID).OrderByDescending(m => m.ID).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.TotalItemCount,
                data = list
            };
            return Json(result);
        }
        

        public void InintForm()
        {
            Expression<Func<CustomForm, bool>> where = m => m.Status != -1;
            ViewBag.CustomFormList = this.GetAll<CustomForm>(where).Select(m => new SelectListItem() { Value = m.ID.ToString(), Text = m.Name }).ToList();
        }

        [HttpGet]
        [MyActionFilterAttribute("001005", "工作委托")]
        public IActionResult Add()
        {
            InintForm();
            return View(new MyDelegation() { StartTime=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") });
        }

        [HttpPost]
        [MyActionFilterAttribute("001005", "工作委托")]
        public IActionResult Add(string toEmpID,string RangeDateTime,List<int> CustomFormID)
        {
            BaseResult baseResult = new BaseResult();
            if(CustomFormID==null|| CustomFormID.Count == 0)
            {
                baseResult.State = 400;
                baseResult.Msg = "请至少选择一个委托单据";
                return View(baseResult);
            }
            DateTime startDateTime, endDateTime;
            if (string.IsNullOrWhiteSpace(RangeDateTime))
            {
                baseResult.State = 400;
                baseResult.Msg = "请选择一个委托时间段";
                return View(baseResult);
            }
            string[] datetimes= RangeDateTime.Split(new string[] { "到"},StringSplitOptions.RemoveEmptyEntries);
            if (datetimes.Length != 2)
            { 
                baseResult.State = 400;
                baseResult.Msg = "请选择一个合法的委托时间段";
                return View(baseResult);
            }

            if(!DateTime.TryParse(datetimes[0],out startDateTime))
            {
                baseResult.State = 400;
                baseResult.Msg = "请选择一个合法的委托时间段";
                return View(baseResult);
            }

            if (!DateTime.TryParse(datetimes[1], out endDateTime))
            {
                baseResult.State = 400;
                baseResult.Msg = "请选择一个合法的委托时间段";
                return View(baseResult);
            }


            if (string.IsNullOrWhiteSpace(toEmpID))
            {
                baseResult.State = 400;
                baseResult.Msg = "请选择一个委托人";
                return View(baseResult);
            }

            Employee infoEmployee=this.dbContextFactory.DbContext.Employee.Where(m => m.EmpID == toEmpID).FirstOrDefault();
            if (infoEmployee == null)
            {
                baseResult.State = 400;
                baseResult.Msg = "请选择的委托人不存在";
                return View(baseResult);
            }
            Expression<Func<CustomForm, bool>> where = m => m.Status != -1;
            var customForms= this.GetAll<CustomForm>(where).Select(m =>new { ID = m.ID,Name=m.Name }).ToList();

             

            List<MyDelegation> models = new List<MyDelegation>();
            foreach (int customFormID in CustomFormID)
            {
                var customForm = customForms.Where(m => m.ID == customFormID).FirstOrDefault();
                if (customForm == null)
                {
                    continue;
                }
                MyDelegation item = new MyDelegation();
                item.CreateDateTime = DateTime.Now;
                item.CustomFormID = customFormID;
                item.CustomFormName = customForm.Name;
                item.EndTime = endDateTime.ToString("yyyy-MM-dd HH:mm:ss");
                item.FromEmpID = Info.EmpID;
                item.FromEmpName = Info.EmpName;
                item.StartTime=startDateTime.ToString("yyyy-MM-dd HH:mm:ss");
                item.Status = 1;
                item.ToEmpID = infoEmployee.EmpID;
                item.ToEmpName = infoEmployee.EmpName;
                models.Add(item);
            }
            if (models.Count == 0)
            {
                baseResult.State = 400;
                baseResult.Msg = "请至少选择一个合法的委托单据";
                return View(baseResult);
            }
            this.AddRange<MyDelegation>(models);
            baseResult.Msg = "委托添加成功";
            return Json(baseResult);
        }

       
        [HttpGet]
        [MyActionFilterAttribute("001005", "工作委托")]
        public IActionResult Cancel(int id)
        {
            BaseResult result = new BaseResult();
            var info = this.dbContextFactory.DbContext.MyDelegation.Where(m => m.ID == id && m.FromEmpID == Info.EmpID).FirstOrDefault();
            if (info == null)
            {
                result.State = 400;
                result.Msg = "没有找到该委托,请刷新页面重试";
            }
            else
            {
                MyDelegation updateMyDelegation = new MyDelegation();
                updateMyDelegation.Status = 2;
                updateMyDelegation.CancelDateTime = DateTime.Now;
                this.Update<MyDelegation>(info, updateMyDelegation, new string[] { "Status", "CancelDateTime" });
            }
            return Json(result);
        }

    }
}